package net.hubbery.web;

import net.hubbery.bean.Bill;
import net.hubbery.service.Orderservice;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet(name = "orderServlet",urlPatterns = "/order")
public class orderServlet extends BaseServlet {
    protected void addOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String custormer = request.getParameter("customer");
        String roomId = request.getParameter("roomId");
        Map<String, String[]> parameterMap = request.getParameterMap();
        Bill bill = new Bill();
        try {
            BeanUtils.populate(bill, parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        Orderservice orderService = new Orderservice();
        try {
            boolean order = orderService.createOrder(bill);
            if (order) {
                response.sendRedirect(request.getContextPath() + "order.jsp");
                System.out.println("success");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    protected void getOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Orderservice orderservice = new Orderservice();
        List<Bill> bills = orderservice.getBills();
        if (bills != null && bills.size() > 0) {
            request.setAttribute("bill_list", bills);
            request.getRequestDispatcher("/order.jsp").forward(request, response);
        }
    }
}
